import { EToolsNames } from '~/Enum/Enum'

const modules = import.meta.glob('~/views/**/*.vue')
const modulesKeys = Object.keys(modules)

const getCom = (name: string) => {
    const _list = modulesKeys.filter((v) => v.startsWith(`/src/views/${name}`))
    let _arr =  _list.reduce((a: any[], b: string) => {
        let _name = b.split(name)[1].replace('.vue', '')
        let _sp = _name.split('-')
        let spdot = '/' + name + _sp[0]
        _sp[0] = _sp[0].substring(1)
        let sort = 1000
        if (_sp.length > 2) {
            sort = parseInt(_sp[2])
            _sp = [_sp[0], _sp[1]]
        }
        let title = _sp.join('-')
        a.push({
            name: spdot,
            path: spdot,
            meta: {
                title,
                sort
            },
            component: modules[b]
        })
        return a
    }, [])
    _arr = _arr.sort((a,b)=>a.meta.sort - b.meta.sort)
    return _arr
}

const _rEToolsNames = ['error', ...EToolsNames]

export default [
    {
        name: 'layout',
        path: '/layout',
        component: () => import('~/views/layout/index.vue'),
        children: _rEToolsNames.reduce((a, b) => {
            const rarr = getCom(b)
            if (rarr.length) {
                a.push({
                    path: '/' + b,
                    redirect: rarr[0].path,
                    name: '/' + b
                })
                a = a.concat(rarr)
            }
            return a
        }, [] as any[])
    }
]
